package com.linktrip.devicemanager.util;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.util.collection.ShortCollections;

/**
 * @author : CaineZhu E-mail:CaineZhu@me.com
 * @version : V1.0
 * @创建时间 ： 2017年5月2日 上午11:05:34
 * @类说明 : blablabla
 */
public class ByteUtil {
	static Logger logger = LoggerFactory.getLogger(ByteUtil.class);

	// short 转byte //低位在前高位在后
	public static byte[] shortToByteArray(short s) {
		byte[] targets = new byte[2];
		for (int i = 0; i < 2; i++) {
			int offset = (targets.length - 1 - i) * 8;
			targets[i] = (byte) ((s >>> offset) & 0xff);
		}
		return targets;
	}
	
	 public static short byte2Short(byte... b){  
	        return (short) (((b[0] & 0xff) << 8) | (b[1] & 0xff));  
	    }  

	// short 转short //高位在前低位在后
	public static short convertShort(short value) {
		short mask = 0x00FF;
		short b, c;
		b = (short) ((value >> 8) & mask);
		c = (short) ((value << 8) & (~mask));
		value = (short) (b | c);
		return value;
	}

	// int 转byte //低位在前高位在后
	public static byte[] int2bytes(int num) {
		byte[] result = new byte[4];
		result[0] = (byte) ((num >>> 24) & 0xff);// 说明一
		result[1] = (byte) ((num >>> 16) & 0xff);
		result[2] = (byte) ((num >>> 8) & 0xff);
		result[3] = (byte) ((num >>> 0) & 0xff);
		return result;
	}

	public static byte[] timeStamp2Bytes(long time, boolean lock) {

		DateTime dt = new DateTime(time);
		short year = (short) dt.getYear();
		short month = (short) dt.getMonthOfYear();
		short day = (short) dt.getDayOfMonth();

		short hours = (short) dt.getHourOfDay();
		short minute = lock ? (short) (dt.getMinuteOfHour() + 1) : (short) (dt.getMinuteOfHour());
		short seconds = (short) dt.getSecondOfMinute();
		short millis = (short) dt.getMillisOfSecond();
		System.out.println(year + " " + month + " " + day + " " + hours + " " + minute + " " + seconds);
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(bos);

		try {
			dos.writeShort(ByteUtil.convertShort(year));
			dos.writeByte(month);
			dos.writeByte(day);
			dos.writeByte(hours);
			dos.writeByte(minute);
			dos.writeByte(seconds);
			dos.writeByte(millis);
			return bos.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {

			try {
				if (null != dos)
					dos.close();
				if (null != bos)
					bos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void printBytes(byte... bs) {
		for (byte b : bs) {
			System.out.print(checkLen(b, 2) + " ");
		}
	}

	public static void loggerBytes(byte... bs) {
		StringBuilder sb = new StringBuilder();
		for (byte b : bs) {
			// System.out.print(checkLen(b,2)+" ");
			sb.append(checkLen(b, 2) + " ");
		}
		logger.info("{}", sb.toString());
	}
	
	public static String bytes2String(byte... bs) {
		StringBuilder sb = new StringBuilder();
		for (byte b : bs) {
			// System.out.print(checkLen(b,2)+" ");
			sb.append(checkLen(b, 2) + " ");
		}
		return sb.toString();
	}

	public static String checkLen(String source, int len) {
		StringBuilder sb = new StringBuilder();
		for (int i = len - source.length(); i > 0; i--) {
			sb.append("0");
		}
		return sb.append(source).toString();
	}

	public static String checkLen(byte source, int len) {
		return checkLen(Integer.toHexString(source & 0xff), len);
	}

	public static void main(String[] args) throws IOException {
		//timeStamp2Bytes(System.currentTimeMillis(), false);
		//System.out.println(byte2Short(new byte[]{0x01,0x6a}));
		//short s = Short.valueOf(checkLen(Integer.toHexString(0xff & 0x01),2) +checkLen(Integer.toHexString(0xff & 0x6a),2),16);
		//System.out.println(s);
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(bos);
		dos.writeShort(convertShort((short)256));
		byte[] bb = bos.toByteArray();
		System.out.println(bytes2String(bb));
		
	}

}
